package cn.aixuegao.web;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.aixuegao.common.base.controller.BaseController;
import cn.aixuegao.common.config.UploadConfig;
import cn.aixuegao.common.result.Response;
import cn.aixuegao.model.admin.Article;
import cn.aixuegao.model.admin.Column;
import cn.aixuegao.service.admin.ArticleService;
import cn.aixuegao.service.admin.ColumnService;

/**
 * 
 * <p>Title: CommonController.java</p>  
 * <p>Description: 常用的请求处理</p>  
 * <p>Company: www.aixuegao.cn</p>  
 * @author hxy  
 * @date 2018年4月20日  下午3:36:14
 * @version 1.0.0
 */
@Controller
public class CommonController extends BaseController{
	@Autowired
	ArticleService articleService;
	@Autowired
	ColumnService columnService;
	
	@RequestMapping(value={"message-board"},method=RequestMethod.GET)
	public String messageBoard(ModelMap model) {
		return "messageBoard";
	}
	@RequestMapping(value={"/index","/"},method=RequestMethod.GET)
	public String index(ModelMap model) {
		EntityWrapper<Article> ew = new EntityWrapper<>();
		ew.setSqlSelect("id,title,publish_time,key_word,author,type");
		ew.where("column_id = {0}", "1");
		ew.orderBy("publish_time", false);
		ew.last("LIMIT 0,3");
		List<Article> live_list = articleService.selectList(ew);
		model.addAttribute("live_list", live_list);
		EntityWrapper<Article> ew2 = new EntityWrapper<>();
		ew2.setSqlSelect("id,title,publish_time,key_word,author,type");
		ew2.in("column_id", "4,5,6,7");
		ew2.orderBy("publish_time", false);
		ew2.last("LIMIT 0,6");
		List<Article> technology_list = articleService.selectList(ew2);
		model.addAttribute("technology_list", technology_list);
		return "index";
	}

	/**
	 * @Description:查询某栏目的列表文章
	 * @param columnId
	 * @param size
	 * @param current
	 * @return
	 */
	@RequestMapping(value="/hiview/{columnId}",method=RequestMethod.GET)
	public String hiview(@PathVariable(value="columnId") Integer columnId,
						 @RequestParam(value="size",defaultValue="5",required = false) Integer size,
						 @RequestParam(value="current",defaultValue="1",required = false) Integer current,
						 ModelMap model) {
	 /*Page<Article> page = new Page<>(current,size);
	 Article article = new Article();
	 article.setColumnId(columnId);
	 EntityWrapper<Article> ew = new EntityWrapper<>(article);
	 ew.setSqlSelect("id,title,digest,publish_time,author");
	 ew.where("type = {0}", "0");
	 Page<Article> p= articleService.selectPage(page, ew);
	 model.addAttribute("article_list", p);*/
	 /*查询栏目名称*/
	 Column column = columnService.selectById(columnId);
	 /*用于查询栏目的id*/
	 model.addAttribute("columnId", columnId);
	 model.addAttribute("column", column);
	 return "hiview";
	}
	
	@RequestMapping(value="/hiview/{columnId}/list",method=RequestMethod.GET)
	@ResponseBody
	public Response hiviewList(@PathVariable(value="columnId") Integer columnId,
						 	   @RequestParam(value="size",defaultValue="5",required = false) Integer size,
						 	   @RequestParam(value="current",defaultValue="1",required = false) Integer current
						 		){
	 Page<Article> page = new Page<>(current,size);
	 Article article = new Article();
	 article.setColumnId(columnId);
	 EntityWrapper<Article> ew = new EntityWrapper<>(article);
	 ew.setSqlSelect("id,title,digest,publish_time,author");
	 ew.where("type = {0}", "0");
	 Page<Article> p= articleService.selectPage(page, ew);
	 return resultPageData(p);
	}
	
	/**
	 * 
	 * @Description:文章详情
	 * @param articleId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/info/{articleId}",method=RequestMethod.GET)
	public String hiviewInfo(@PathVariable(value="articleId") Integer articleId,ModelMap model) {
		Article artacle = articleService.selectById(articleId);
		Column column = columnService.selectById(artacle.getColumnId());
		model.addAttribute("article", artacle);
		model.addAttribute("column", column);
		return "info";
	}
	
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public String test() {
		return "test";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="/contact-us",method=RequestMethod.GET)
	public String contactUs() {
		return "contactUs/contactUs";
	}
}
